package com.tbit.uqbike.webmanager.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.tbit.common.utils.TraceUtil;
import com.tbit.uqbike.annos.OperateLog;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.uqbike.object.pojo.AdoptMachineBound;
import com.tbit.uqbike.object.pojo.dto.CommonDTO;
import com.tbit.uqbike.object.pojo.vo.BatchOperationMsgVO;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.business.AdoptMachineBoundService;
import com.tbit.uqbike.webmanager.util.Assert;
import com.tbit.uqbike.webmanager.util.excel.ExcelUtil;
import com.tbit.uqbike.webmanager.util.excel.ExcelUtils;
import com.tbit.utils.NullUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * @Author: chen
 * @Description: 车辆领养
 * @Date: 创建于 10:50 2020/8/14
 */
@Slf4j
@RestController
@RequestMapping("/adopt")
public class AdoptMachineBoundController {
    @Autowired
    private AdoptMachineBoundService adoptMachineBoundService;

    public static void main(String[] args) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println(simpleDateFormat.parse("2020-10-30").getTime());
    }

    /**
     * 分页
     *
     * @param name
     * @param userCode
     * @param accountId
     * @param pageNO
     * @param rowCount
     * @return
     */
    @RequestMapping("/queryPage")
    public JsonResult queryPage(String name, String userCode, Integer accountId, Integer accountUserId,
                                @RequestParam(defaultValue = "1") Integer pageNO, @RequestParam(defaultValue = "10") Integer rowCount) {
        List<AdoptMachineBound> list = adoptMachineBoundService.queryPage(name, userCode, accountId, accountUserId, pageNO, rowCount);
        Integer total = adoptMachineBoundService.queryCount(name, userCode, accountId, accountUserId);
        return JsonResult.ok(list, total);
    }

    /**
     * 导出车辆信息
     *
     * @param name
     * @param userCode
     * @param accountId
     * @return
     */
    @RequestMapping("/exportVehicle")
    public JsonResult exportVehicle(String name, String userCode, Integer accountId, Integer accountUserId) {
        List<AdoptMachineBound> list = adoptMachineBoundService.queryPage(name, userCode, accountId, accountUserId, 1, 65535);
        ExcelUtil<AdoptMachineBound> excelUtil = new ExcelUtil<>(AdoptMachineBound.class);
        return excelUtil.exportExcel(list, "领养车辆信息");

    }

    /**
     * 添加
     *
     * @param adoptMachineBound
     * @param userCode
     * @return
     */
    @RequestMapping("/insert")
    @OperateLog(module = ModuleEnum.ADOPT, methods = MethodEnum.ADOPT_ADD)
    public JsonResult insert(AdoptMachineBound adoptMachineBound, String userCode) {
        Assert.notNull(adoptMachineBound.getAccountUserId(), "ver.accountUserId.notNull");
        Assert.notNull(userCode, "ver.userCode.notNull");
        List<String> list = Arrays.asList(userCode.split(","));
        return JsonResult.ok(adoptMachineBoundService.insert(adoptMachineBound, list));
    }

    /**
     * 删除
     *
     * @param adoptMachineBound
     * @return
     */
    @RequestMapping("/del")
    @OperateLog(module = ModuleEnum.ADOPT, methods = MethodEnum.ADOPT_DEL)
    public JsonResult del(AdoptMachineBound adoptMachineBound) {
        Assert.notNull(adoptMachineBound.getAccountUserId(), "ver.accountUserId.notNull");
        Assert.notNull(adoptMachineBound.getMachineId(), "ver.machineId.notNull");
        adoptMachineBoundService.del(adoptMachineBound);
        return JsonResult.ok();
    }


    /**
     * 批量删除领养人车辆
     * @param adoptMachineBound adoptMachineBound
     * @return Result
     */
    @PostMapping("/batchDel")
    @OperateLog(module = ModuleEnum.ADOPT, methods = MethodEnum.ADOPT_DEL)
    public Result<BatchOperationMsgVO> batchDel(@RequestBody AdoptMachineBound adoptMachineBound){
        if (CollectionUtil.isEmpty(adoptMachineBound.getUserCodes()) || ObjectUtil.isNull(adoptMachineBound.getAccountUserId())){
            return Result.error("请选择需要删除的领养人车辆和领养人信息");
        }
        return adoptMachineBoundService.batchDel(adoptMachineBound);
    }



    /**
     * 导出
     *
     * @return
     */
    @RequestMapping("/excel")
    @OperateLog(module = ModuleEnum.ADOPT, methods = MethodEnum.ADOPT_EXCEL)
    public JsonResult excel(Integer accountUserId, Long startTime, Long endTime, HttpServletResponse response) {
        Assert.notNull(accountUserId, "ver.accountUserId.notNull");
        Assert.notNull(startTime, "ver.startTime.notNull");
        Assert.notNull(endTime, "ver.endTime.notNull");

        List<CommonDTO> list = adoptMachineBoundService.getAdoptMoney(accountUserId, startTime, endTime);
        if (NullUtils.isEmpty(list)) {
            JsonResult.fail("查询数据为空", TraceUtil.getTraceIdByLocal());
        }

        LinkedHashMap<String, String> Total = new LinkedHashMap<String, String>();
        Total.put("date", "日期");
        Total.put("money", "收入");
        LinkedHashMap<String, String> titleMap = Total;
        try {
            ExcelUtils.listToExcel(list, titleMap, "领养人收入", response);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return JsonResult.ok();
    }
}
